package to4.client;

import java.util.ArrayList;

import com.google.gwt.core.client.JsArray;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.visualization.client.DataTable;
import com.google.gwt.visualization.client.Selection;
import com.google.gwt.visualization.client.AbstractDataTable.ColumnType;
import com.google.gwt.visualization.client.events.SelectHandler;
import com.google.gwt.visualization.client.visualizations.Table;

public class ZonderVervoer1 {
	public TO4 myService;
	private ZonderHome ZH;
	private DataTable myData = DataTable.create();
	private Table chart = new Table();
	private int klik = 0;
	private int geselecteerderij;
	private ArrayList<String> emailArray;
	public ZonderVervoer1(ZonderHome thema5){
		this.ZH = thema5;
	}
	public Panel createPage(final String email) {
		emailArray = new ArrayList<String>();
		FlowPanel eindPanel = new FlowPanel();
		final VerticalPanel vervoerVertical = new VerticalPanel();
		vervoerVertical.addStyleName("borderVerticalVervoer");
		Label test2 = new Label("Beschikbare auto's:");

		test2.addStyleName("chapterReg");
		if (myData.getNumberOfColumns()==0) {
			myData.addColumn(ColumnType.STRING, "Auto");
			myData.addColumn(ColumnType.STRING, "Type");
			myData.addColumn(ColumnType.STRING, "Provincies");
			myData.addColumn(ColumnType.STRING, "Reizen");
			myData.addColumn(ColumnType.STRING, "Roken");
			myData.addColumn(ColumnType.STRING, "Fiets");
			myData.addColumn(ColumnType.STRING, "Plaats");
		}
		final VerticalPanel test3 = new VerticalPanel();
		vervoerVertical.add(test2);
		vervoerVertical.add(test3);
		serverCall(email);
		vervoerVertical.add(gegevensOpvragen());
		if (klik == 0){
		}  
		chart.addSelectHandler(new SelectHandler() {  
			@Override
			public void onSelect(SelectEvent event) {
				klik++;
				JsArray<Selection> selections = chart.getSelections();
				for (int i = 0; i < selections.length(); i++) {
					Selection selection = selections.get(i);
					geselecteerderij = selection.getRow();
				}

				AsyncCallback<Boolean> autoVol = new AsyncCallback<Boolean>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSuccess(Boolean result) {
						if(result == true){
							Window.alert("Auto is vol");
						}
						if(result == false){
							ZH.showZonderVervoer2(email, emailArray.get(geselecteerderij));
							System.out.print(emailArray.get(geselecteerderij));
						}
					}
				};
				ZH.thema.myService.autoVol(emailArray.get(geselecteerderij), autoVol);
			}
		});  

		eindPanel.add(vervoerVertical);
		return eindPanel;
	}

	public Table gegevensOpvragen() {
		chart.draw(myData);
		return chart;

	}

	public void serverCall(String email) {
		AsyncCallback<ArrayList<String>> opvragen = new AsyncCallback<ArrayList<String>>(){

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
			}
			@Override
			public void onSuccess(ArrayList<String> result) {
				emailArray.clear();
				if (myData.getNumberOfRows() == 0) {
					for(String r:result){
						String[] gesplitteResultaat = r.split("\\:\\*\\:");
						emailArray.add(gesplitteResultaat[7]);
						int rijnummer = myData.addRow();
						for (int i=0;i<7;i++){
							myData.setValue(rijnummer, i, gesplitteResultaat[i]);
						}
						chart.draw(myData);
					}
				}
				else {
					myData.removeRows(0, myData.getNumberOfRows());
					for(String r:result){
						String[] gesplitteResultaat = r.split("\\:\\*\\:");
						emailArray.add(gesplitteResultaat[7]);
						int rijnummer = myData.addRow();
						for (int i=0;i<7;i++){
							myData.setValue(rijnummer, i, gesplitteResultaat[i]);
						}
						chart.draw(myData);
					}
				}
			}
		};
		ZH.thema.myService.gebruikersMetAuto(email, opvragen);

	}
}